package com.ceo.auth.jwt.wrapper;


import com.ceo.auth.jwt.JwtEntity;
import com.ceo.auth.jwt.JwtUtil;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.function.Function;

/**
 * @Create by hefy
 * @Date 2022/1/10 12:42
 */
public class JwtWrapper implements Wrapper<Object> {

    private HttpServletRequest request;

    private HttpServletResponse response;

    private HandlerMethod handler;

    private JwtEntity jwtEntity;

    private Function<Object, Object> tmpData;

    private int index = -1;

    @Override
    public boolean support(Object handler) {
        return handler != null && handler instanceof HandlerMethod && !(((HandlerMethod)handler).getBean() instanceof BasicErrorController);
    }

    public int doNext() {
        return ++index;
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    public HandlerMethod getHandler() {
        return handler;
    }

    public void setHandler(HandlerMethod handler) {
        this.handler = handler;
    }

    public JwtEntity getJwtEntity() {
        return jwtEntity;
    }

    public Function<Object, Object> getTmpData() {
        return tmpData;
    }

    public void setTmpData(Function<Object, Object> tmpData) {
        this.tmpData = tmpData;
    }

    public void setJwtEntity(JwtEntity jwtEntity) {
        this.jwtEntity = jwtEntity;
    }

    public Boolean responseState(int status) {
        JwtUtil.responseState(status,response);
        return Boolean.FALSE;
    }
}
